package com.schoolComments.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.schoolComments.dto.Result;
import com.schoolComments.dto.UserDTO;
import com.schoolComments.entity.Follow;
import com.schoolComments.mapper.FollowMapper;
import com.schoolComments.mapper.UserMapper;
import com.schoolComments.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolComments.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private FollowMapper followMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;
    @Override
    //根据id查询是否关注了该用户
    public Result followOrNot(Long id) {
        //这里直接去redis中查吧 redis中差不到结果在去db查(db中查到了就写到redis) db查不到就是真没关注了
        //准备key
        Long id1 = UserHolder.getUser().getId();
        String key = "user:follow:"+id1;
        //在redis中查
        Boolean isFollow = stringRedisTemplate.opsForSet().isMember(key, id+"");
        if (BooleanUtil.isFalse(isFollow)) {
            //没查到
            //去db查
            Integer count = followMapper.isFollow(id1, id);
            if (count != 1) {
                //没查到
                return Result.ok(false);
            }
            //查到
            //写入redis
            stringRedisTemplate.opsForSet().add(key, id+"");
            return Result.ok(true);
        }
        //查到
        return Result.ok(true);
    }

    @Override
    //关注/取关某一个用户
    public Result follow(Long id, Boolean isFollow) {
        //准备key
        Long id1 = UserHolder.getUser().getId();
        String key = "user:follow:"+id1;
        if (isFollow) {
            //关注
            //写入redis
            stringRedisTemplate.opsForSet().add(key, id+"");
            //写入db
            Integer count = followMapper.insertFollow(id1, id);
            if (count == 1) {
                return Result.ok();
            }
        }
        //取关
        //从redis中删除相关value
        stringRedisTemplate.opsForSet().remove(key, id+"");
        //从db中删除
        Integer count = followMapper.deleteFollow(id1, id);
        return Result.ok();
    }

    @Override
    //查找共同关注
    public Result commonLike(Long id) {
        //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        //准备key
        String userKey = "user:follow:"+userId;
        String anotherUserKey = "user:follow:"+id;
        //在redis中查看共同关注的id
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(userKey, anotherUserKey);
        //没有交集
        if (intersect == null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        //有交集
        //根据id查询用户 封装成userDto返回
        List<UserDTO> userDTOS = userMapper.queryUsersByIds(intersect);
        return Result.ok(userDTOS);
    }






}
